$(document).ready(function() {

    function show() {
        var menu = $(this);
        menu.children(".submenus").css('display','inline-block');
    }

    function hide() {
        var menu = $(this);
        menu.children(".submenus").css('display','none');
    }

    $(".hover-menu").hoverIntent({
        sensitivity: 1,
        interval: 50,
        over: show,
        timeout: 600,
        out: hide
    });

    function showdiv() {
        var menu = $(this);
        menu.children(".hover-menu-div").css('display','inline-block');
    }

    function hidediv() {
        var menu = $(this);
        menu.children(".hover-menu-div").css('display','none');
    }

    $(".hover-menu").hoverIntent({
        sensitivity: 1,
        interval: 50,
        over: showdiv,
        timeout: 600,
        out: hidediv
    });


    get_rss_feed($("#newsitems"),'rss.xml');
});

function checkForm(){
    var correct = $("#name").val().length > 0 &&
    $("#company").val().length > 0 &&
    $("#email").val().match(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/);

    if(!correct){
        alert("U moet alle velden correct invullen");
    }
    
    return correct;
}

function get_rss_feed(target, FeedURL) {
    var limit = 4;

    var regSpace = new RegExp("[ ]", "g");
    var regA10 = new RegExp("[T-]", "g");
    var tabMonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    var date = new Date();
    var month = tabMonth[date.getMonth()];
    var day = date.getDate();

    $.get(FeedURL, function(d) {

        //var feedTitle = $(d).find('title:first').text();
        //var feedLink = $(d).find('link:first').text();
        //var html = "<a href=" + feedLink + "><span class=\"title\">" + feedTitle + "</span></a>";
        var html = "";
        var loop = 1;

        var items = $(d).find('item');
        
        if (items.length == 0)
            items = $(d).find('entry');

        $(items).each(function() {
            if (loop == limit)
                return false;

            var $item = $(this);
            var title = $item.find('title').text();
            //var link = $item.find('link').text();
            var pubDate = $item.find('pubDate');
            var description = $item.find('description').text();

            var fullDate = '';

            if (pubDate.length == 0) {
                pubDate = $item.find('a10\\:updated');
                if (pubDate.length == 0)
                    pubDate = $item.find('updated');
                pubDate = pubDate.text().split(regA10);
                fullDate = pubDate[2] + '-' + pubDate[1] + '-' + pubDate[3];
            }
            else {
                pubDate = pubDate.text().split(regSpace);
                var mon = $.inArray(pubDate[2],tabMonth);
                if (mon < 10){
                    mon = '0' + mon;
                }else{
                    mon = '' + mon;
                }
                fullDate = pubDate[1] + '-' + mon + '-' + pubDate[3];
            }


            /*if (pubDate[2] == month && pubDate[1] == day) {
                fullDate = "<b>" + fullDate + "<\/b>";
            }*/

            //html += "<a href=\"" + link + "\" target=\"_blank\">" + fullDate + " - " + title + "<\/a><br\/>";
            html+= '<li class=\"newsitem\">';
            html+= '<div class=\"newstitle\">'+title+'</div>';
            html+= '<p class=\"newsdate\">'+fullDate+'</p>';
            html+= '<p class=\"newscontent\">'+description+'</p></li>';
            
            loop++;
        });

        // clear the destination div
        $(target).empty();
        // populate the div
        $(target).append($(html));
    });
}